import java.awt.*;
import java.awt.event.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.swing.*;
import java.io.*;

/*
 * Handles the login process (encryption, assigning an ID#, create a new login)
 */
public class Login extends JPanel {

	private GUIPatient pinterface;
	
	private JLabel welcome;
	private JPanel panel;
        private JPanel center;
	private JTextField utext;
	private JTextField ptext;
	private JButton login;
	private JButton logout;
	private Member current;
	private MemberData list;
	private JTabbedPane jPane;
        private JPanel south;
	
	
	//CONSTRUCTOR FOR LOGIN PANE
	//
	public Login(Member c, MemberData a, JTabbedPane j)
	{
		
		list = a;
		
		welcome = new JLabel("Welcome to CareWare. Please Login.");
                FileInputStream fileInput = null;
                ObjectInputStream inStream = null;
                
                try
                {
                    fileInput = new FileInputStream("memberlist");
                    inStream = new ObjectInputStream(fileInput);
                    Object obj = inStream.readObject();
                    
                    if(obj instanceof MemberData)
                    {
                        
                        
                        list = (MemberData) obj;
                        inStream.close();
                        //welcome.setText("imported");
                    }
                    
                }
                catch(FileNotFoundException ex)
                {
                	welcome.setText("File not found");
                }
                catch(IOException ex)
                {
                welcome.setText("io");}
                catch(ClassNotFoundException ex)
                {
                welcome.setText("class not found");}

               
                
		jPane = j;
		current = c;
		
		
		utext = new JTextField("Username");
		ptext = new JTextField("Password");
		login = new JButton("       Login   ");
		logout = new JButton("       Logout");
		panel = new JPanel();
	
		//utext.setMaximumSize(new Dimension (200,100));
		//ptext.setMaximumSize(new Dimension (200,100));
		
		//panel.setPreferredSize(new Dimension(500,470));
		panel.setLayout(new BorderLayout());
		//Color background = new Color(204,255,255);
		//panel.setBackground(background);
                center = new JPanel();
                center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));
                south = new JPanel();
                south.setLayout(new BoxLayout(south, BoxLayout.Y_AXIS));
		panel.add(welcome, BorderLayout.NORTH);
		
		center.add(utext);
		center.add(ptext);
		panel.add(center, BorderLayout.CENTER);
		south.add(login);
                south.add(logout);
		panel.add(south, BorderLayout.SOUTH);
                
		logout.setEnabled(false);
		
		this.add(panel);
		
		LoginListener li = new LoginListener();
		login.addActionListener(li);
		LogoutListener lo = new LogoutListener();
		logout.addActionListener(lo);
	}
	
	
	//LOGIN BUTTON LISTENER
	//grabs test from both text fiels. Verifies login. Displays error and resets on incorrect login.
	//adds tabs based on usertype.
	 private class LoginListener implements ActionListener
     {
             public void actionPerformed(ActionEvent event)
             {
                     String username = utext.getText();
                     byte[] password = Crypto.getHash(ptext.getText()); 
                     
                     if(list.validate(username, password) !=null)
                     {
                             current = list.validate(username, password);
                             
                             //adds relevant panes if user is a doctor
                             if(list.validate(username, password) instanceof Doctor)
                             {
                                     GUIDoctor temp = new GUIDoctor((Doctor) current, list, jPane);
                                     jPane.addTab("New Patient", new GUINewPatient(current, list, temp));
                                     jPane.addTab("Actions", temp);
                                     

                             }
                             
                             if(list.validate(username, password) instanceof Patient)
                             {
                                     jPane.addTab("Update Health", new GUIPatient((Patient)current));
                                     jPane.addTab("Change Info", new GUIPatientInfo((Patient) current));
                             }
                             
                             
                             if(list.validate(username, password) instanceof Nurse)
                             {
                                     GUINurse temp = new GUINurse((Nurse) current, list, jPane);
                                     jPane.addTab("New Patient", new GUINewPatientNurse(current, list, temp));
                                     jPane.addTab("Actions", temp);
                             }
                             
                             //enables and disables buttons, updates text
                             login.setEnabled(false);
                             logout.setEnabled(true);
                             welcome.setText("You are logged in");   
                     }
                     
                     else
                     {
                             current = null;
                             welcome.setText("Login incorrect");
                             utext.setText("Username");
                             ptext.setText("Password");
                     }
             }
     }
	
	
	//LOGOUT BUTTON LISTENER
	//Resets all textfields. Sets current login to null. deletes all panes except the login pane.
	private class LogoutListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			current = null;
			welcome.setText("Welcome to CareWare. Please Login.");
			utext.setText("Username");
			ptext.setText("Password");
			login.setEnabled(true);
			logout.setEnabled(false);
			
			while(jPane.getTabCount() > 1)
                        {
                            jPane.remove(1);
                        }
                        
			FileOutputStream fileOutput = null;
			ObjectOutputStream outStream = null;
			
			try
			{
                           
				fileOutput = new FileOutputStream("memberlist");
                                
				outStream = new ObjectOutputStream(fileOutput);
				outStream.writeObject(list);
				outStream.close();
				//welcome.setText("File was written");
			}
			catch(NotSerializableException ex)
			{
			
				welcome.setText(ex.toString());
			
			}
			catch(IOException ex)
			{
				welcome.setText(ex.toString());
			}
			
			finally
			{	
				try
				{
					if(outStream !=null)
					{
						outStream.close();
					}
					
				}
				catch(IOException ex)
				{
                                welcome.setText(ex.toString());}
			
			}
		}
	}
}
